草庐IT

C++ 11:std::thread池化?

全部标签

c++ - 不持有锁的本地静态初始化避免了 C++11 中可能出现的死锁?

论文http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2008/n2660.htm提出了一种算法,该算法在局部静态变量的初始化期间不需要持有锁,但仍会导致通过变量定义的并发控制流等待初始化完成。论文说这样做的好处是避免了可能出现的死锁Thecoreproblemwithfunction-localstatic-durationobjectinitializationisthatthecontainingfunctionmaybeinvokedconcurrently,andthusthedefinitionmayexecuteconc

C++11 for_each 和 lambdas 优化

我正在测试以下代码:#include#include#include#includeintmain(intargc,char*argv[]){std::vectorv(10000000);clock_tthen=clock();if(argc我正在使用g++4.6编译它,没有任何优化标志,这是我得到的:[javadyan@myhostexperiments]$./a.out260000[javadyan@myhostexperiments]$./a.outaaa330000[javadyan@myhostexperiments]$使用-O1优化会产生以下(意料之中的)结果:[javad

c++ - solaris (x86) 上std::basic_string 的一些疑惑

solaris(x86)上std::basic_string的一些困惑#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws(s,s+6);for(inti=0;i运行结果为:9799101000为什么不是979899100101102代码#include#includeintmain(){constwchar_t*s=L"abcdef";std::wstringws;ws.resize(6);for(inti=0;i可以得到预期的结果。我使用gcc3.4.6,构建命令是g++-fshort-wcharstri

c++ - std::bad_alloc 之后 std::vector 的状态

我试图找到一个在线引用来查看几个标准容器的异常安全性。在std::vector的情况下,它是否保持push_back调用之前的状态?我假设vector的所有对象仍然有效(没有调用析构函数)。在push_back抛出std::bad_alloc异常后,提供什么保证std::vector? 最佳答案 如果它抛出,vector不会改变。甚至不是capacity()。根据[container.requirements.general]:Unlessotherwisespecified(see23.2.4.1,23.2.5.1,23.3.3.

c++ - 如何在 Metro 应用程序中打印到 C++ 中的 Visual Studio 11 控制台/日志?

我正在尝试弄清楚如何在VisualStudio11C++Metro应用程序中打印到调试器控制台/日志。我尝试了多种方法来打印到C++Metro应用程序中的日志(cout、Windows.System.Diagnostics、MSDN和Metro开发站点上的文档)。如何打印到控制台? 最佳答案 OutputDebugString:Sendsastringtothedebuggerfordisplay.某些WindowsAPI可从Metro风格应用程序使用,包括此功能。您可以从Metro风格应用程序中找到其他与诊断相关的WindowsA

c++ - std::call_once 和内存重新排序

给定来自here的代码:classlazy_init{mutablestd::once_flagflag;mutablestd::unique_ptrdata;voiddo_init()const{data.reset(newexpensive_data);}public:expensive_dataconst&get_data()const{std::call_once(flag,&lazy_init::do_init,this);return*data;}};我在其他地方也看到了相同模式的一些变体。所以我的问题是:为什么这段代码被认为是保存的?以及为什么编译器不能在调用std::c

c++ - std::unique_ptr<T> 不完整类型错误

我有templateclassqueue{private:structnode{Tdata;std::unique_ptrnext;//compileerroronincompletetypenode(T&&data_):data(std::move(data_)){}};std::unique_ptrhead;node*tail;public:queue():tail(nullptr){}我在VS10的标记行上遇到编译错误。在这种情况下,我是否应该被允许使用不完整的类型(实例化模板-构造函数-这里以int为例)?有解决方法吗?编辑singlethreadedqueue.h(62):e

c++ - 命名空间 std 中所有符号的列表

关闭。这个问题不符合StackOverflowguidelines.它目前不接受答案。我们不允许提问寻求书籍、工具、软件库等的推荐。您可以编辑问题,以便用事实和引用来回答。关闭6年前。Improvethisquestion我在哪里可以找到在命名空间std中声明的所有符号的列表?实在找不到,不知道去哪里找。

c++ - 在 WaitForMultipleObjects() 中使用 std::vector

我有一个句柄对象的std::vector。我必须等待这些句柄对象才能在WaitForMultipleObjects函数中使用。因为它是一个vector,所以在WaitForMultipleObjects中使用它时出现错误:std::vectorevents;//...WaitForMultipleObjects(events.size(),events,true,INFINITE);有什么办法吗? 最佳答案 最好,如果你有最新版本的STL,你应该使用:WaitForMultipleObjects(events.size(),even

c++11 统一初始化不适用于 clang++

我从thiswikipediapage复制了以下示例:structBasicStruct{intx;doubley;};structAltStruct{AltStruct(intx,doubley):x_{x},y_{y}{}private:intx_;doubley_;};BasicStructvar1{5,3.2};AltStructvar2{2,4.3};intmain(intargc,charconst*argv[]){return0;}然后我尝试用编译它clang++-Wall-std=c++11test.cpp但是我得到这个错误:test.cpp:17:11:error:n